package cliqueproblem;

import java.io.IOException;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

import org.jgrapht.UndirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.ext.DOTExporter;
import org.jgrapht.ext.VertexNameProvider;

public class DotExport {
	UndirectedGraph<Vertex, DefaultEdge> ExpGraph;
	String path;

	public DotExport(UndirectedGraph<Vertex, DefaultEdge> g, String p) {
		ExpGraph = g;
		path = p;
	}

	public boolean export() {
		DOTExporter<Vertex, DefaultEdge> exportToDot =
			new DOTExporter<Vertex, DefaultEdge>(new NameProvider(), new LabelProvider(), null);
		try {
			FileOutputStream outputStream = new FileOutputStream(path);
			OutputStreamWriter writer = new OutputStreamWriter(outputStream, "Cp1251");
			BufferedWriter bufWriter = new BufferedWriter(writer);
			exportToDot.export(bufWriter, ExpGraph);
			bufWriter.close();
			return true;
		} catch (IOException e) {
			System.out.println("Can not write into file " + path);
			e.printStackTrace();
			return false;
		}
	}

	private class NameProvider implements VertexNameProvider<Vertex> {
		public String getVertexName(Vertex v) {
			return v.toString();
		}
	}

	private class LabelProvider implements VertexNameProvider<Vertex> {
		public String getVertexName(Vertex v) {
			if (v.getIsClique())
				return "Max Clique Member";
			return "Not in clique";
		}
	}
}
